<?php

header("Content-type: text/plain");
header("Cache-Control: no-store, no-cache");

include_once("banco.php");

$mes = $_GET["mes"];
$ano = $_GET["ano"];

switch($mes) {
    case "0":
        $mes = "01";
        break;
    case "1":
        $mes = "02";
        break;
    case "2":
        $mes = "03";
        break;
    case "3":
        $mes = "04";
        break;
    case "4":
        $mes = "05";
        break;
    case "5":
        $mes = "06";
        break;
    case "6":
        $mes = "07";
        break;
    case "7":
        $mes = "08";
        break;
    case "8":
        $mes = "09";
        break;
    case "9":
        $mes = "10";
        break;
    case "10":
        $mes = "11";
        break;
    case "11":
        $mes = "12";
        break;
}

$link = pg_connect(URL);

$query = "
    select
        f.nome,
        sum(v.quantidade)
    from funcionario f
    join venda v on
        v.id_funcionario = f.id
    where
        v.data like '$ano-$mes-__'
    group by
        f.nome
    ";
$result = pg_query($link, $query);
if(pg_num_rows($result) > 0) {
    $res = array();
    while (list($nome, $quantidade) = pg_fetch_array($result)) {
        $res[] = "$nome:$quantidade";
    }
    $quant = 3;
    for($x = 0; $x < $quant; $x++) {
        if($res[$x] == "") {
            $res[$x] = " :0";
        }
        if($x == $quant - 1) {
            echo $res[$x];
        }else {
            echo $res[$x].",";
        } 
    }
}else {
    echo "ER";
}

?>
